package GUILayer;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import ModelLayer.*;
import ControlLayer.*;


public class Delivery extends JFrame {

	private JPanel contentPane;
	
	private IFSearch search;
	private IFCreate create;
	private IFUpdate update;
	private ModelLayer.Delivery d;
	private JTextField txtDd;
	private JTextField txtMm;
	private JTextField txtYy;
	private boolean errorD;
	private boolean errorM;
	private boolean errorY;
	private Date deliveryDate;

	
	
	/**
	 * Create the frame.
	 */
	public Delivery(boolean isNew, final int id) {
		
		search = new SearchControler();
		create = new CreateControler();
		update = new UpdateControler();
		
		setResizable(false);
		if(isNew)
		{
			setTitle("New Delivery");
		}
		else
		{
			setTitle("Delivery");
			d = new ModelLayer.Delivery();
			d = search.getDeliveryById(id);
		}
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(150, 50, 225, 155);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 94, 14);
		contentPane.add(lblId);
		
		JLabel lblShowID = new JLabel("");
		lblShowID.setBounds(94, 11, 116, 14);
		contentPane.add(lblShowID);
		lblShowID.setText(Integer.toString(id));
		
		JLabel lblDeliveryDate = new JLabel("Delivery date:");
		lblDeliveryDate.setBounds(10, 36, 94, 14);
		contentPane.add(lblDeliveryDate);

		txtDd = new JTextField();
		txtDd.setBounds(94, 33, 30, 20);
		contentPane.add(txtDd);
		txtDd.setColumns(10);
		if(isNew)
		{
			txtDd.setText("dd");
		}
		else
		{
			txtDd.setText(DateFormat.getInstance().format(d.getDeliveryDate()).substring(0,2));
		}
		
		JLabel label = new JLabel("/");
		label.setBounds(128, 36, 12, 14);
		contentPane.add(label);
		
		txtMm = new JTextField();
		txtMm.setBounds(134, 33, 30, 20);
		contentPane.add(txtMm);
		txtMm.setColumns(10);
		if(isNew)
		{
			txtMm.setText("mm");
		}
		else
		{
			txtMm.setText(DateFormat.getInstance().format(d.getDeliveryDate()).substring(3,5));
		}
		
		JLabel label_1 = new JLabel("/");
		label_1.setBounds(168, 36, 12, 14);
		contentPane.add(label_1);
		
		txtYy = new JTextField();
		txtYy.setBounds(175, 33, 30, 20);
		contentPane.add(txtYy);
		txtYy.setColumns(10);
		if(isNew)
		{
			txtYy.setText("yy");
		}
		else
		{
			txtYy.setText(DateFormat.getInstance().format(d.getDeliveryDate()).substring(6,8));
		}
		
		JLabel lblStatus = new JLabel("Status:");
		lblStatus.setBounds(10, 61, 94, 14);
		contentPane.add(lblStatus);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Created" , "In progress" , "Delivered" , "Canceled"}));
		comboBox.setBounds(94, 58, 116, 20);
		contentPane.add(comboBox);
		if(!isNew)
		{
			switch(d.getDeliveryState())
			{
			case "created": comboBox.setSelectedIndex(0);
				break;
			case "in progress" : comboBox.setSelectedIndex(1);
				break;
			case "delivered" : comboBox.setSelectedIndex(2);
				break;
			case "canceled": comboBox.setSelectedIndex(3);
				break;
			}
		}
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveNewDelivery(id, comboBox.getSelectedItem().toString(), txtDd.getText(), txtMm.getText(), txtYy.getText());
			}
		});
		btnSave.setBounds(10, 89, 89, 23);
		contentPane.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(121, 89, 89, 23);
		contentPane.add(btnCancel);
	}
	
	private void saveNewDelivery(int deliveryId, String deliveryState, String day, String month, String year)
	{
		if(deliveryState.isEmpty() || day.isEmpty() || month.isEmpty() || year.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorD = false;
		    try
		    {
		    	Integer.parseInt(day);
		    }
		    catch(Exception e)
		    {
		    	JOptionPane.showMessageDialog(null,
                        "The day should be a number!",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
    			errorD = true;
		    }
		    if(errorD!=true)
		    {
		    	errorM = false;
		    	try
		    	{
		    		Integer.parseInt(month);
		    	}
		    	catch(Exception e)
		    	{
		    		JOptionPane.showMessageDialog(null,
                            "The month should be a number!",
                            "Warning",
                            JOptionPane.WARNING_MESSAGE);
        			errorM = true;
		    	}
		    	if(errorM!=true)
		    	{
		    		errorY=false;
		    		try
		    		{
		    			Integer.parseInt(year);
		    		}
		    		catch(Exception e)
		    		{
		    			JOptionPane.showMessageDialog(null,
                                "The year should be a number!",
                                "Warning",
                                JOptionPane.WARNING_MESSAGE);
            			errorY = true;
		    		}
		    		if(errorY!=true)
		    		{
		    			SimpleDateFormat formatter =  new SimpleDateFormat("ddmmyy");
		    			try {
		    				deliveryDate = formatter.parse(day + month + year);
		    			} catch (ParseException e) {
		    				JOptionPane.showMessageDialog(null,
	                                "Date not valid!",
	                                "Warning",
	                                JOptionPane.WARNING_MESSAGE);
		    				e.printStackTrace();
		    			}
		    			ModelLayer.Delivery d = new ModelLayer.Delivery(deliveryId, deliveryState, deliveryDate);
		    	        try {
							create.createDelivery(d);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(null,
	                                "Something went wrong, please try again!",
	                                "Warning",
	                                JOptionPane.WARNING_MESSAGE);
							e.printStackTrace();
						}
		    	        MainWindow.getInstance().refreshTableDelivery();
		    	        dispose();
		    		}
		    	}
		    }
		}
	}
	
	private void saveDelivery(int deliveryId, String deliveryState, String day, String month, String year)
	{
		if(deliveryState.isEmpty() || day.isEmpty() || month.isEmpty() || year.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorD = false;
		    try
		    {
		    	Integer.parseInt(day);
		    }
		    catch(Exception e)
		    {
		    	JOptionPane.showMessageDialog(null,
                        "The day should be a number!",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
    			errorD = true;
		    }
		    if(errorD!=true)
		    {
		    	errorM = false;
		    	try
		    	{
		    		Integer.parseInt(month);
		    	}
		    	catch(Exception e)
		    	{
		    		JOptionPane.showMessageDialog(null,
                            "The month should be a number!",
                            "Warning",
                            JOptionPane.WARNING_MESSAGE);
        			errorM = true;
		    	}
		    	if(errorM!=true)
		    	{
		    		errorY=false;
		    		try
		    		{
		    			Integer.parseInt(year);
		    		}
		    		catch(Exception e)
		    		{
		    			JOptionPane.showMessageDialog(null,
                                "The year should be a number!",
                                "Warning",
                                JOptionPane.WARNING_MESSAGE);
            			errorY = true;
		    		}
		    		if(errorY!=true)
		    		{
		    			SimpleDateFormat formatter =  new SimpleDateFormat("ddmmyy");
		    			try {
		    				deliveryDate = formatter.parse(day + month + year);
		    			} catch (ParseException e) {
		    				JOptionPane.showMessageDialog(null,
	                                "Date not valid!",
	                                "Warning",
	                                JOptionPane.WARNING_MESSAGE);
		    				e.printStackTrace();
		    			}
		    			ModelLayer.Delivery d = new ModelLayer.Delivery(deliveryId, deliveryState, deliveryDate);
		    	        update.updateDelivery(d);
		    		}
		    	}
		    }
		}
	}
}
